<template>
	<view class="page">
		<nav-bar-sub></nav-bar-sub>
		<view class="questionPage">
			<view class="left">
				<view class="avatar">
					<img loading="lazy" :src="data.imgPath">
				</view>
				<view class="name">
					{{data.stuName}}
				</view>
			</view>
			<view class="right">
				<view class="QAbox">
					<view class="QAcontent">
						<view class="top">
							<view class="QAtitle">
								{{data.title}}
							</view>
							<view class="time">
								{{data.lastModifiedTime}}
							</view>
						</view>
						<view class="main">
							<view class="text">
								<u-parse :html="data.content"></u-parse>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="replyButton">
				<u-button @click="reply">回复</u-button>
			</view>
		</view>
		<view class="gapline">

		</view>
		<view class="replyPage">
			<view class="replyTop">
				<view class="sidebar"></view>
				<view class="replyNum">
					全部回复({{data.totalQue}})
				</view>
			</view>
			<view class="replyContent" v-for="(reply,index) in replylist">
				<view class="left">
					<view class="avatar">
						<img loading="lazy" :src="reply.imgPath">
					</view>
					<view class="name">
						{{reply.stuName || reply.stuSecondName}}
					</view>
				</view>
				<view class="right">
					<view class="QAbox">
						<view class="QAcontent">
							<view class="top">
								<view class="time">
									{{reply.lastModifiedTime}}
								</view>
							</view>
							<view class="main">
								<view class="text">
									<u-parse :html="reply.content"></u-parse>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view>
			<u-popup v-model="popupReply" mode="center" length="60%" border-radius='8'>
				<view class="replyWrap">
					<view class="replyContent">
						<u-input v-model="replyText" type='textarea' />
					</view>
					<view class="replyBottom">
						<view class="editbutton">
							<u-button type="default" @click="replyFinish()">完成</u-button>
						</view>
					</view>
				</view>
			</u-popup>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				data: {},
				replylist: [],
				popupReply: false,
				replyText: ''
			}
		},
		methods: {
			reply() {
				console.log(this.data);
				this.popupReply = true
			},
			replyFinish() {
				if (!this.replyText) {
					this.$refs.uToast.show({
						title: '文本不能为空',
						type: 'warning'
					})
					return null
				} else {
					let data = this.data
					this.$api.addQuestions(this.$store.state.access_token, data.lessonId, data.id, this.replyText, null)
						.then(res => {
							console.log(res);
							this.popupReply = false
							this.$refs.uToast.show({
								title: '回复成功',
								type: 'success'
							})
							this.replyText = ''
							let pages = getCurrentPages(); //获取所有页面栈实例列表
							let prevPage = pages[pages.length - 2]; //上一页页面实例
							setTimeout(() => {
								prevPage.$vm.refreshQA()
							}, 1200)
							this.data.totalQue++
							this.replylist.push(res.values)
						})
				}
			}
		},
		onLoad: function(data) {
			console.log(data);
			data.imgPath =
				`${this.$store.state.base_url}/system/loadImg?imgPath=${encodeURI(data.stuImgPath)}&accessToken=${this.$store.state.access_token}`
			this.data = data
			this.$api.getMyQuestionAnswer(this.$store.state.access_token, data.id)
				.then(res => {
					this.replylist = res.values.questions
					this.replylist.forEach((item, index) => {
						item.imgPath =
							`${this.$store.state.base_url}/system/loadImg?imgPath=${encodeURI(item.stuImgPath)}&accessToken=${this.$store.state.access_token}`
					})
					console.log(this.replylist);
				})
		}
	}
</script>

<style lang="scss">
	.page {
		display: flex;
		flex-direction: column;

		.popupReply {
			box-sizing: border-box;
			padding: 50rpx;
			width: 500rpx;
			height: 500rpx;

			.replyWrap {
				padding: 50rpx;

				.replyBottom {
					display: flex;
					flex-direction: row;

					.bottomBlank {
						flex: 2;
					}

					.editbutton {
						flex: 1
					}
				}
			}
		}

		.questionPage {
			display: flex;
			flex-direction: row;
			box-sizing: border-box;
			padding: 5rpx 5rpx 80rpx 5rpx;
			position: relative;

			.replyButton {
				position: absolute;
				bottom: 5rpx;
				right: 5rpx;
			}

			.left {
				display: flex;
				flex-direction: column;
				align-items: center;
				box-sizing: border-box;

				image {
					width: 90rpx;
					height: 90rpx;
					border-radius: 50%;
					background-color: #f2f2f2;
				}
			}

			.right {
				.QAbox {
					box-sizing: border-box;
					padding: 10rpx 10rpx 10rpx 10rpx;
					box-sizing: border-box;
					margin: 10px;
					box-shadow: 0 0 5px 1px rgba($color: #000000, $alpha: 0.1);
					border-radius: 5px;

					.QAcontent {
						display: flex;
						flex-direction: column;

						.top {
							display: flex;
							justify-content: space-between;
							align-items: center;

							.QAtitle {
								font-weight: bold;
							}

							.time {
								padding-left: 15rpx;
								color: #BED3ED;
							}
						}

						.main {
							box-sizing: border-box;
							padding: 10rpx 5rpx 0rpx 5rpx;
							width: 100%;
						}
					}
				}
			}
		}

		.gapline {
			width: 100%;
			background-color: #EEEEEE;
			height: 10rpx;
			margin-bottom: 10rpx;
		}

		.replyPage {
			display: flex;
			flex-direction: column;
			box-sizing: border-box;
			padding: 5rpx;

			.replyTop {
				display: flex;
				align-items: center;

				.sidebar {
					box-sizing: border-box;
					margin-left: 25rpx;
					background-color: #006DD1;
					width: 10rpx;
					height: 34rpx;
					border-radius: 10rpx;
				}

				.replyNum {
					box-sizing: border-box;
					padding-left: 15rpx;
					font-size: 34rpx;
				}
			}

			.replyContent {
				display: flex;
				flex-direction: row;
				box-sizing: border-box;
				padding: 5rpx;

				.left {
					display: flex;
					flex-direction: column;
					align-items: center;
					box-sizing: border-box;

					image {
						width: 90rpx;
						height: 90rpx;
						border-radius: 50%;
						background-color: #f2f2f2;
					}
				}

				.right {
					.QAbox {
						box-sizing: border-box;
						padding: 10rpx 10rpx 10rpx 10rpx;
						box-sizing: border-box;
						margin: 10px;
						box-shadow: 0 0 5px 1px rgba($color: #000000, $alpha: 0.1);
						border-radius: 5px;

						.QAcontent {
							display: flex;
							flex-direction: column;

							.top {
								display: flex;
								justify-content: space-between;
								align-items: center;

								.time {
									color: #BED3ED;
								}
							}

							.main {
								box-sizing: border-box;
								padding: 10rpx 5rpx 0rpx 5rpx;
								width: 100%;
							}
						}
					}
				}
			}



		}
	}

	.avatar {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50rpx;
		overflow: hidden;

		img {
			width: 100%;
			height: 100%;
		}
	}
</style>
